﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities.Forms
{
    public partial class NewSupply : Form
    {
        int userid;
        int supplyId;
        Form fo = null;
        public NewSupply(int id,Form m)
        {
            fo = m;
            this.MdiParent = m;
            userid = id;
            InitializeComponent();
            AcceptButton = btn_Add_new;
        }
        public NewSupply(int id, Form m,int supid)
        {
            supplyId = supid;
            fo = m;
            this.MdiParent = m;
            userid = id;
            InitializeComponent();
            AcceptButton = btn_Modefy;

        }
        ModelContainer ctx = new ModelContainer();
        public bool is_loaded = false;
        int? selectedId;
        private void NewSupply_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            cbo_Supplies.DataSource = ctx.Supplies.ToList();
            cbo_SupplyType.DataSource = ctx.SupplyTypes.ToList();
            dtp_DueExpire.Value = DateTime.Now.AddYears(1);
            if (supplyId > 0)
            {
                Supply s = ctx.Supplies.Single(w => w.Id == supplyId);
                cbo_Supplies.SelectedValue = s.Id;
                txb_ammount.Text = s.Amount.ToString();
                if (s.DateToExpires != null)
                    dtp_DueExpire.Value = (DateTime)s.DateToExpires;
                cbo_SupplyType.SelectedValue = s.SupplyTypeId;
                cbo_unit.Text = s.UnitOfMeasurement;
            }
           
           
          

        }

        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btn_Add_new_Click(object sender, EventArgs e)
        {
            try
            {

                var supplyList=ctx.Supplies.Where(c=>c.Name==cbo_Supplies.Text);
                if (supplyList.Count() == 0)
                {
                    Supply s = new Supply();
                    double ammount;
                    if (double.TryParse(txb_ammount.Text, out ammount))
                    {
                        s.Amount = ammount;
                    }
                    s.Name = cbo_Supplies.Text;
                    s.UnitOfMeasurement = cbo_unit.Text;
                    s.DateToExpires = dtp_DueExpire.Value;
                    s.InsertDate = DateTime.Now;
                    s.InsertUId = userid;
                    var st = ctx.SupplyTypes.Where(d => d.Name == cbo_SupplyType.Text);
                    if (st.Count() > 0)
                    {
                        s.SupplyTypeId = (int)cbo_SupplyType.SelectedValue;
                        ctx.Supplies.Add(s);
                    }
                    else
                    {
                        DialogResult result = MessageBox.Show("قيمة حقل نوع المعونة غير موجودة هل تريد إدخال نوع معونة جديد","حقل نوع المعونة غير موجود",MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            SupplyType stype = new SupplyType();
                            stype.Name = cbo_SupplyType.Text;
                            stype.InsertDate = DateTime.Now;
                            stype.InsertUId = userid;
                            stype.Supplies.Add(s);
                            ctx.SupplyTypes.Add(stype);
                        }
                    }
                    ctx.SaveChanges();
                    cbo_Supplies.DataSource = null;
                    cbo_Supplies.DataSource = ctx.Supplies.ToList<Supply>();
                    cbo_SupplyType.DataSource = ctx.SupplyTypes.ToList<SupplyType>();
                    cbo_Supplies.DisplayMember = "Name";
                    cbo_Supplies.ValueMember = "Id";
                    cbo_SupplyType.DisplayMember = "Name";
                    cbo_SupplyType.ValueMember = "Id";
                }
                else
                {
                    MessageBox.Show("لايمكن اضافة العنصر الموجود مسبقاً");
                }
            }
            catch
            {

            }
        }

        private void btn_Modefy_Click(object sender, EventArgs e)
        {
            try
            {
            
                if (selectedId != null && selectedId > 0)
                {
                    Supply sup = ctx.Supplies.Single(c => c.Id == selectedId);
                    sup.Name = cbo_Supplies.Text;
                    sup.DateToExpires = dtp_DueExpire.Value;
                    sup.SupplyTypeId = (int)cbo_SupplyType.SelectedValue;
                    double ammount;
                    if (double.TryParse(txb_ammount.Text, out ammount))
                    {
                        sup.Amount = ammount;
                    }
                    sup.UnitOfMeasurement = cbo_unit.Text;
                    sup.UpdateDate = DateTime.Now;
                    sup.UpdateUId = userid;
                    ctx.SaveChanges();
                }
               
            }
            catch 
            {
                
            }
        }
        

        private void btn_Save_Click(object sender, EventArgs e)
        {

        }

        private void cbo_Supplies_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            if (cbo_Supplies.SelectedValue != null && (int)cbo_Supplies.SelectedValue>0)
            {
                selectedId = (int)cbo_Supplies.SelectedValue;
                Supply sup = ctx.Supplies.Single(c => c.Id == selectedId);
                cbo_Supplies.Text = sup.Name;
                if (sup.DateToExpires != null)
                    dtp_DueExpire.Value = (DateTime)sup.DateToExpires;
                txb_ammount.Text = sup.Amount.ToString();
                cbo_unit.Text = sup.UnitOfMeasurement;
                cbo_Supplies.DisplayMember = "Name";
                cbo_Supplies.ValueMember = "Id";
                cbo_SupplyType.SelectedValue = sup.SupplyTypeId;
                btn_Add_new.Enabled = false;
                btn_delete.Enabled = true;
                btn_Modefy.Enabled = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int id = (int)cbo_Supplies.SelectedValue;
           Supply sup = ctx.Supplies.Single(c => c.Id == id);
            if(sup.GrantDetails.Count()>0||sup.CartDetails.Count()>0)
                MessageBox.Show("لايمكن حذف مادة معطاة منها منحة أو مسجلة ضمن سلة معونات");
            else
            {
                ctx.Supplies.Remove(sup);
                ctx.SaveChanges();
                cbo_Supplies.DataSource = null;
                cbo_Supplies.DataSource = ctx.Supplies;
            }
        }

        private void cbo_Supplies_TextChanged(object sender, EventArgs e)
        {
           
            btn_Add_new.Enabled = true;
            btn_Modefy.Enabled = true;
            btn_delete.Enabled = false;
            
        }

        private void NewSupply_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void NewSupply_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
